package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.SMSUtils;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 获取验证码
 *
 * @author zz
 * @since 2022/11/22 21:14
 */

@Slf4j
@RestController
//@RequestMapping("/mobile/validateCode")
public class ValidateCodeController {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @PostMapping("/send")
    public Result send(@RequestBody String telephone,@RequestBody String type){
        log.info("手机号--->{}",telephone);
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",telephone,code);
        redisTemplate.opsForValue().set(telephone,code,5, TimeUnit.MINUTES);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
}
